#!/usr/bin/env python
# -*- coding: utf-8 -*-

class Validate(object):
    def __init__(self):
        self.msg = 'ok'
    #假设check_len为已废弃的方法，我们希望保留其原来的代码同时使用新的实现check_len2
    def check_len(self, text):
        if len(text) < 3 or len(text) > 20:
            raise Exception("len must be greater than 3 and less than 20")
        return True

    def __getattribute__(self, attr_name):
        if attr_name == 'check_len':
            print('if')
            return self.check_len2
        else:
            print('else')
            return super().__getattribute__(attr_name)

    def check_len2(self, text):
        if isinstance(text, str):
            if len(text) < 3 or len(text) > 20:
                raise Exception("len must be greater than 3 and less than 20")
            return self.msg
        raise TypeError("must be str type")

validate = Validate()
ret = validate.check_len("i love python")
print(ret) #"if\nelse\nelse\nok"
#print(validate.check_len("io")) #Exception: len must be greater than 3 and less than 20
#print(validate.check_len([1,2,3])) #TypeError: must be str type